/**
 * js继承
 */

//--------------------原型链继承---------------------------
function Parents(name) {
  this.name = name
  this.array = [1, 2, 3]
}

Parents.prototype.sing = function () {
  console.log(this.name, ':sing')
}

function Child(name) {
  this.name = name
}

// 原型链继承
Child.prototype = new Parents()
Child.prototype.constructor = Child

let child = new Child('zhangsan')
console.log('a', child.sing())
/*zhangsan :sing
a undefined*/

//-------------------构造函数继承------------------------------
function Person(name) {
  this.name = name
  this.array = [1, 2, 3]
}

Person.prototype.dump = function () {
  console.log('dump:', this.name)
}

function Student(name) {
  Person.call(this)
  this.name = name
}

let xiaoming = new Student('xiaoming')
// console.log(xiaoming.dump())  //报错
console.log(xiaoming)
// Student { name: 'xiaoming' }

// ---------------------组合继承-----------------------------
function Parent(name) {
  this.name = name
  this.array = [1, 2, 3]
}

Parent.prototype.sing = function () {
  console.log(this.name, ':sing')
}

function Child0(name) {
  Parent.call(this)
  this.name = name
}

// 原型链继承
Child0.prototype = new Parent()
Child0.prototype.constructor = Child0

let child0 = new Child0('zhangsan')
console.log('c', child0)

//-----------------------原型继承-----------------------------
function Teacher(name) {
  this.name = name
}

Teacher.prototype = Object.create(Person.prototype)
let teacher = new Teacher('xiaohong')
console.log('teacher', teacher)
//teacher Person { name: 'xiaohong' }

//-------------------寄生组合继承------------------------------
// 父类
function Parent1() {
  this.name = ['父类']
  this.introduce = function () {
    console.log("父类上的introduce方法")
  }
}

Parent1.prototype.sayhi = function () {
  console.log('父类原型上的sayhi方法');
}

// 子类
function Child1() {
  this.childname = ['子类']
  Parent1.call(this) // 核心代码
}

Child1.prototype = Object.create(Parent1.prototype) // 核心代码

const child1 = new Child1()
const child2 = new Child1()
child1.name[0] = 'child1'
child2.name[0] = 'child2'
console.log(child1);
console.log(child1.name);
console.log(child2.name);